/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.app.modular.dynamic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.app.modular.dynamic.param.front.ApiGetDynamicListParam;
import vip.xiaonuo.app.modular.dynamic.param.front.ApiSendDynamicParam;
import vip.xiaonuo.app.modular.dynamic.result.GetDynamicListResult;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.app.modular.dynamic.entity.AppDynamic;
import vip.xiaonuo.app.modular.dynamic.enums.AppDynamicExceptionEnum;
import vip.xiaonuo.app.modular.dynamic.mapper.AppDynamicMapper;
import vip.xiaonuo.app.modular.dynamic.param.mgr.AppDynamicParam;
import vip.xiaonuo.app.modular.dynamic.service.AppDynamicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 朋友圈service接口实现类
 *
 * @author 郑雄伟
 * @date 2023-10-17 15:46:28
 */
@Service
public class AppDynamicServiceImpl extends ServiceImpl<AppDynamicMapper, AppDynamic> implements AppDynamicService {

    @Resource
    private SysFileInfoService sysFileInfoService;

    @Override
    public void sendDynamic(ApiSendDynamicParam param) {
        AppDynamic appDynamic = new AppDynamic();
        BeanUtil.copyProperties(param, appDynamic);
        this.save(appDynamic);
    }

    @Override
    public List<GetDynamicListResult> getDynamicList(ApiGetDynamicListParam param) {
        //条件构造器
        QueryWrapper<GetDynamicListResult> queryWrapper = new QueryWrapper<>();
        List<GetDynamicListResult> dynamicList = this.baseMapper.getDynamicList(PageFactory.defaultPage(), queryWrapper).getRecords();
        //数据组装
        dynamicList.forEach(e->{
            //头像
            String headUrl = sysFileInfoService.getSysFileInfo(e.getHeadPicId());
            e.setHeadPicUrl(headUrl);
            //发朋友圈的内容
            if (ObjectUtil.isNotEmpty(e.getFileIds())){
                List<String> fileUrl = sysFileInfoService.getListSysFileInfo(Arrays.asList(e.getFileIds().split(",")));
                e.setFileUrl(fileUrl);
            }
        });
        return dynamicList;
    }

    @Override
    public PageResult<AppDynamic> page(AppDynamicParam appDynamicParam) {
        QueryWrapper<AppDynamic> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(appDynamicParam)) {

            // 根据动态内容 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getContent())) {
                queryWrapper.lambda().eq(AppDynamic::getContent, appDynamicParam.getContent());
            }
            // 根据文件id 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getFileIds())) {
                queryWrapper.lambda().eq(AppDynamic::getFileIds, appDynamicParam.getFileIds());
            }
            // 根据1正常 2屏蔽(删除) 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getStatus())) {
                queryWrapper.lambda().eq(AppDynamic::getStatus, appDynamicParam.getStatus());
            }
            // 根据用户id 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getUserId())) {
                queryWrapper.lambda().eq(AppDynamic::getUserId, appDynamicParam.getUserId());
            }
            // 根据经度 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getLon())) {
                queryWrapper.lambda().eq(AppDynamic::getLon, appDynamicParam.getLon());
            }
            // 根据纬度 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getLat())) {
                queryWrapper.lambda().eq(AppDynamic::getLat, appDynamicParam.getLat());
            }
            // 根据城市 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getCity())) {
                queryWrapper.lambda().eq(AppDynamic::getCity, appDynamicParam.getCity());
            }
            // 根据1图片 2视频 3文本 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getType())) {
                queryWrapper.lambda().eq(AppDynamic::getType, appDynamicParam.getType());
            }
            // 根据点赞 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getLike())) {
                queryWrapper.lambda().eq(AppDynamic::getLike, appDynamicParam.getLike());
            }
            // 根据转发 查询
            if (ObjectUtil.isNotEmpty(appDynamicParam.getForward())) {
                queryWrapper.lambda().eq(AppDynamic::getForward, appDynamicParam.getForward());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<AppDynamic> list(AppDynamicParam appDynamicParam) {
        return this.list();
    }

    @Override
    public void add(AppDynamicParam appDynamicParam) {
        AppDynamic appDynamic = new AppDynamic();
        BeanUtil.copyProperties(appDynamicParam, appDynamic);
        this.save(appDynamic);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<AppDynamicParam> appDynamicParamList) {
        appDynamicParamList.forEach(appDynamicParam -> {
            this.removeById(appDynamicParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(AppDynamicParam appDynamicParam) {
        AppDynamic appDynamic = this.queryAppDynamic(appDynamicParam);
        BeanUtil.copyProperties(appDynamicParam, appDynamic);
        this.updateById(appDynamic);
    }

    @Override
    public AppDynamic detail(AppDynamicParam appDynamicParam) {
        return this.queryAppDynamic(appDynamicParam);
    }

    /**
     * 获取朋友圈
     *
     * @author 郑雄伟
     * @date 2023-10-17 15:46:28
     */
    private AppDynamic queryAppDynamic(AppDynamicParam appDynamicParam) {
        AppDynamic appDynamic = this.getById(appDynamicParam.getId());
        if (ObjectUtil.isNull(appDynamic)) {
            throw new ServiceException(AppDynamicExceptionEnum.NOT_EXIST);
        }
        return appDynamic;
    }

    @Override
    public void export(AppDynamicParam appDynamicParam) {
        List<AppDynamic> list = this.list(appDynamicParam);
        PoiUtil.exportExcelWithStream("SnowyAppDynamic.xls", AppDynamic.class, list);
    }

}
